﻿package ${Package}.bo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import ${Package}.dao.${table.TableNameAlias}Dao;
import ${Package}.domain.${table.TableNameAlias};
import ${Package}.utils.ReturnResult;
import ${Package}.utils.Status;
	#foreach($c in $ColumnList)
	#if($c.IsPrimary)
	#set ($primaryKeyAlias=$c.ColumnNameAlias)
	#set ($primaryKey=$c.ColumnName)
	#end
    #end
/*
*author:徐洪昌
*blog:http://cnblogs.com/xusir
*datetime:${Utils.GetDate} ${Utils.GetTime}
*/ 
public class ${table.TableNameAlias}Bo  extends MyBatisSession {
    /**
	 * 添加数据
	 */
	public ReturnResult Add(${table.TableNameAlias} entity) {
		ReturnResult result = new ReturnResult();
		SqlSession session = getSqlSession();
		${table.TableNameAlias}Dao dao = session.getMapper(${table.TableNameAlias}Dao.class);
		try {
			dao.insert(entity);
			session.commit();
			result.setMsg("添加成功");
			result.setStatus(Status.SUCCESS);
		} catch (Exception e) {
			session.rollback();
			log.error(e.getMessage());
			result.setMsg("添加失败");
			result.setStatus(Status.ERROR);
		} finally {
			session.close();
		}
		return result;
	}

	/**
	 * 删除数据
	 */
	public ReturnResult Delete(String[] idArr) {
	ReturnResult result = new ReturnResult();
		SqlSession session = getSqlSession();
		${table.TableNameAlias}Dao dao = session.getMapper(${table.TableNameAlias}Dao.class);
		try {
			dao.deleteForInIds(idArr);
			session.commit();
			result.setMsg("删除成功");
			result.setStatus(Status.SUCCESS);
		} catch (Exception e) {
			session.rollback();
			result.setMsg("删除失败");
			result.setStatus(Status.ERROR);
			log.error(e.getMessage());
		} finally {
			session.close();
		}
		return result;
	}

	/**
	 * 修改数据
	 */
	public ReturnResult Edit(${table.TableNameAlias} entity) {
		SqlSession session = getSqlSession();
		${table.TableNameAlias}Dao dao = session.getMapper(${table.TableNameAlias}Dao.class);
		ReturnResult result = new ReturnResult();
		try {
			dao.update(entity);
			session.commit();
			result.setMsg("修改成功");
			result.setStatus(Status.SUCCESS);
		} catch (Exception e) {
			session.rollback();
			result.setMsg("修改失败");
			result.setStatus(Status.ERROR);
			log.error(e.getMessage());
		} finally {
			session.close();
		}
		return result;
	}

	/**
	 * 
	 * 数据列表
	 */
	public ReturnResult List(int pageIndex, int pageSize,Map<String, Object> map)
	 {
		ReturnResult result = new ReturnResult();
		SqlSession session = getSqlSession();
		${table.TableNameAlias}Dao dao = session.getMapper(${table.TableNameAlias}Dao.class);
		int recordNum=(pageIndex-1)*pageSize;
		List<${table.TableNameAlias}> lst = session.selectList(
				"${Package}.dao.${table.TableNameAlias}Dao.selectAllByPage", map,
				new RowBounds(recordNum, pageSize));
		int total =dao.countAllForPage(map);
		result.setMsg("返回列表成功");
		result.setStatus(Status.SUCCESS);
		result.setTotal(total);
		result.setRows(lst);
		session.close();
		return result;
	}

	/**
	 * 详细数据信息
	 * 
	 * @param id
	 *            记录的ID
	 * @return
	 */
	public ReturnResult View(int id) {
		SqlSession session = getSqlSession();
		${table.TableNameAlias}Dao dao = session.getMapper(${table.TableNameAlias}Dao.class);
		${table.TableNameAlias} entity = dao.selectOne(id);
		ReturnResult result = new ReturnResult();
		result.setMsg("返回数据详细内容成功！");
		result.setStatus(Status.SUCCESS);
		result.setData(entity);
		session.close();
		return result;
	}

	/**
	 * 更新记录状态
	 * 
	 * @param idArr
	 *            批量更新的ID
	 * @param status
	 *            要更新的状态
	 * @return
	 */
	public ReturnResult Audit(String[] idArr, int status) {
		ReturnResult result = new ReturnResult();		
		SqlSession session = getSqlSession();
		${table.TableNameAlias}Dao dao = session.getMapper(${table.TableNameAlias}Dao.class);
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("idArr", idArr);
			map.put("status", status);
			dao.audit(map);
			session.commit();
			result.setMsg("更新成功!");
			result.setStatus(Status.SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			session.rollback();
			result.setMsg("更新失败:" + e.getMessage());
			result.setStatus(Status.ERROR);
		} finally {
			session.close();
		}
		return result;
	}

}
